package com.xdog.web3.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xdog.web3.config.Web3ConfigService;
import com.xdog.web3.domain.price.CandlesQuery;
import com.xdog.web3.domain.price.PriceRequest;
import com.xdog.web3.domain.price.TradesQuery;
import com.xdog.web3.service.IWeb3MarketService;
import com.xdog.web3.utils.Web3ApiUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Web3行情服务实现类
 */
@Slf4j
@Service
public class Web3MarketServiceImpl implements IWeb3MarketService {

    @Resource
    private Web3ConfigService web3ConfigService;

    @Override
    public JSONObject getPrice(List<PriceRequest> priceRequests) {
        String requestPath = "/api/v6/dex/market/price";
        return Web3ApiUtil.post(requestPath, priceRequests);
    }

    @Override
    public JSONObject getTrades(TradesQuery query) {
        String requestPath = "/api/v6/dex/market/trades";

        Map<String, String> params = new HashMap<>();
        // 只有当chainIndex未提供时才设置默认值
        if (query.getChainIndex() != null && !query.getChainIndex().isEmpty()) {
            params.put("chainIndex", query.getChainIndex());
        }
        //转小写
        params.put("tokenContractAddress", query.getTokenContractAddress().toLowerCase());

        if (query.getAfter() != null) {
            params.put("after", query.getAfter());
        }

        if (query.getLimit() != null) {
            params.put("limit", query.getLimit());
        }

        return Web3ApiUtil.get(requestPath, params);
    }

    @Override
    public JSONObject getCandles(CandlesQuery query) {
        String requestPath = "/api/v6/dex/market/candles";

        Map<String, String> params = new HashMap<>();
        // 只有当chainIndex未提供时才设置默认值
        if (query.getChainIndex() != null && !query.getChainIndex().isEmpty()) {
            params.put("chainIndex", query.getChainIndex());
        }
        params.put("tokenContractAddress", query.getTokenContractAddress());

        if (query.getAfter() != null) {
            params.put("after", query.getAfter());
        }

        if (query.getBefore() != null) {
            params.put("before", query.getBefore());
        }

        if (query.getBar() != null) {
            params.put("bar", query.getBar());
        }

        if (query.getLimit() != null) {
            params.put("limit", query.getLimit());
        }

        return Web3ApiUtil.get(requestPath, params);
    }

    @Override
    public JSONObject getHistoricalCandles(CandlesQuery query) {
        String requestPath = "/api/v6/dex/market/historical-candles";

        Map<String, String> params = new HashMap<>();
        // 只有当chainIndex未提供时才设置默认值
        if (query.getChainIndex() != null && !query.getChainIndex().isEmpty()) {
            params.put("chainIndex", query.getChainIndex());
        }
        params.put("tokenContractAddress", query.getTokenContractAddress());

        if (query.getAfter() != null) {
            params.put("after", query.getAfter());
        }

        if (query.getBefore() != null) {
            params.put("before", query.getBefore());
        }

        if (query.getBar() != null) {
            params.put("bar", query.getBar());
        }

        if (query.getLimit() != null) {
            params.put("limit", query.getLimit());
        }

        return Web3ApiUtil.get(requestPath, params);
    }
}
